package com.sso.entity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tools.util.StrUtils;

public class Page<T> {
	public static final String DEFAULT_PAGE_NO="1";
	public static final String DEFAULT_PAGE_SIZE = "10";

	private int pageNo;		//当前页
	private int pageSize;	//分页数
	private long totalCount;	//总条数
	private int first;		//首行
	private int last;		//尾行
	private List<T> data;	//数据
	private Map<String,Object> paramMap;

	private Page(){

	}

	/**
	 * 传入一个参数Map
	 * @param paramMap
     */
	public Page(Map<String,Object> paramMap){
		this(paramMap==null ?"":(String) paramMap.get("pageNo"),paramMap==null ? "" :(String) paramMap.get("pageSize"));
		this.paramMap = paramMap==null ? new HashMap<String, Object>() : paramMap;
	}

	/**
	 * 传入分页信息
	 * @param pageNo
	 * @param pageSize
     */
	public Page(String pageNo,String pageSize){
		this.pageSize = StrUtils.isEmpty(pageSize) ? Integer.parseInt(this.DEFAULT_PAGE_SIZE) : Integer.parseInt(pageSize);
		this.pageNo = StrUtils.isEmpty(pageNo) ? Integer.parseInt(this.DEFAULT_PAGE_NO) :Integer.parseInt(pageNo);
		this.paramMap = new HashMap<String, Object>();
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List<T> getData() {
		return data;
	}
	public void setData(List<T> data) {
		this.data = data;
	}
	public long getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Long totalCount) {
		if(totalCount==null){
			this.totalCount=0;
		}else {
			this.totalCount = totalCount;
		}
	}
	public int getFirst() {
		return (this.pageNo-1)*pageSize+1;
	}
	public int getLast() {
		return this.pageNo*this.pageSize;
	}

	/**
	 * 获取参数Map,自动封装分页信息
	 * @return
     */
	public Map<String, Object> getParamMap() {
		this.paramMap.put("first", this.getFirst());
		this.paramMap.put("last", this.getLast());
		return paramMap;
	}
}
